package com.wuyou.mid;

/**
 * 743. 网络延迟时间
 *
 * @Date 2024/11/25 10:10
 * @Author 无忧
 */
public class NetworkDelayTime {


    public static void main(String[] args) {
        int[][] times = {{1,2,1},{2,3,2},{1,3,2}};
        int n = 3, k = 1;
        System.out.println(sol_1(times, n, k));
    }
    /**
     * times[i] = (ui, vi, wi)  ui 是源节点，vi 是目标节点， wi 是一个信号从源节点传递到目标节点的时间
     *
     * @param times
     * @param n
     * @param k
     * @return
     */
    public static int sol_1(int[][] times, int n, int k) {
        int l = 0, r = 0, idx = 0, count = 0;
        for (int i = 0; i < times.length; i++) {
            if (times[i][0] == k) {
                idx = i;
                break;
            }
        }
        if (idx == 0 && times[0][0] != k) {
            return -1;
        }
        while (count < times.length) {
            if (times[idx][1] > n) {
                return -1;
            }
            if (times[idx][1] > k) {
                r += times[idx][2];
            } else if (times[idx][1] < k) {
                l += times[idx][2];
            }
            count++;
            idx++;
            if (idx == times.length) {
                idx = 0;
            }
        }
        return Math.max(l, r);
    }
}
